package com.ncp.servlet.registerlogin;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ncp.model.biz.registerlogin.LoginBiz;
import com.ncp.model.entity.registerlogin.UserBasicBean;

public class LoginServlet extends HttpServlet 
{
	private static final long serialVersionUID = 1L;


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException 
	{
		this.doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException 
	{
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		/*
		 * 判断是否登录成功！
		 */
		PrintWriter out = response.getWriter();
		String msg = "";//捕获用户登录时可能发生的提示信息。
		int number = 1;//捕获用户错误输入密码的次数
		LoginBiz loginbiz=new LoginBiz();
		HttpSession session = request.getSession(true);
		String userLoginName = request.getParameter("userLoginName");
		String passWord = request.getParameter("passWord").toString();;
		String inputCheckCode=request.getParameter("inputCheckCode");
		


		if(!inputCheckCode.equalsIgnoreCase((String)session.getAttribute("randCheckCode")))
		{
			msg = "验证码输入错误！";
			out.print("<script language=javascript>alert('"+msg+"');history.go(-1)</script>"); 
			//request.getRequestDispatcher("login.jsp").forward(request, response);
		}
		else
		{
			UserBasicBean ubb = loginbiz.isRegisterUser(userLoginName);	
			/***检查是否为注册用户***/
			if(ubb.getUserLoginName()!=null)
			{
				/*** 是注册用户，则判断密码是否正确
				 * -1代表密码输入错误。
				 * 1代表密码输入正确。 ****/
				session.setAttribute("UserImg", ubb.getUserImg());
				String Shapassword =ubb.getPassWord();
				int checkuserresult =loginbiz.checkUserPsd(Shapassword, passWord) ;
				if(checkuserresult==1)
				{
					/***根据不同的返回值，判断对应的状态信息
					 * -1代表尚未审批。
					 * 1代表用户是审批用户. ****/
					String status = ubb.getStatus();
					int levelid = ubb.getLevelID();
					int approvedresult=loginbiz.isApprovedUser(status);
					if(approvedresult==1)
					{
						session.setAttribute("UserImg", ubb.getUserImg());
						session.setAttribute("userID", ubb.getUserID()+"");
						session.setAttribute("LoginName", userLoginName);
						session.setMaxInactiveInterval(180000);
						session.setAttribute("LevelID", levelid+"");
						request.getRequestDispatcher("index.jsp").forward(request, response);
					}
					else if(approvedresult==-1)
					{
						msg = "对不起，用户尚未激活，请激活后再登录！";
						out.print("<script language=javascript>alert('"+msg+"');history.go(-1)</script>");
					  	request.getRequestDispatcher("index.jsp").forward(request, response);
					}
				}
				else
				{
					
					
					if(session.getAttribute("number")==null)
					{
						 session.setAttribute("number",number);
					}
					else 
					{
						   number = Integer.parseInt(session.getAttribute("number").toString());
						   number++;
						   session.setAttribute("number",number);
					}
					//这里解决获得审批的用户密码输入次数为3次则退出系统的处理。
			        if(number==3)
			        {
			        	msg = "密码输入次数达到3次，系统自动退出！";
			        	out.print("<script language=javascript>alert('"+msg+"');top.window.opener=null;top.window.open('','_self');top.window.close();</script>");
			        }
			        else
			        {
						msg = "密码输入错误！";
						out.print("<script language=javascript>alert('"+msg+"');history.go(-1)</script>");
						
			        }
			    	request.getRequestDispatcher("index.jsp").forward(request, response);
				}
			}
			else
			{
				msg = "用户尚未注册！请注册后登录！";
				out.print("<script language=javascript>alert('"+msg+"');window.parent.location.href='RegisterServlet'</script>");
				request.getRequestDispatcher("index.jsp").forward(request, response);
			}
		}
	
		
	}
}